/**
 * 
 */
package com.ofek.eventos.db;

import java.io.Serializable;
import java.util.ArrayList;

import android.content.Context;

import com.ofek.base.db.DataField;
import com.ofek.base.db.DataTypeClass;
import com.ofek.eventos.EventosApplication;
import com.ofek.eventos.db.datatypes.predefined.ContextDefiner;
import com.ofek.eventos.db.datatypes.predefined.PageDefiner;
import com.ofek.eventos.util.IOUtils;

/**
 * @author  Ofek
 */
@DataTypeClass
public class ContextWrapper implements ContextDefiner {
	/**
	 * 
	 */
	@DataField(isKey=true,isAutoIncrement=true)
	public long id;
	@DataField
	public String serial;
	public ContextDefiner realDefiner;
	/**
	 * 
	 */
	public ContextWrapper() {
	}
	public ContextWrapper(final Serializable serial) {
		this.serial = IOUtils.objectToString(serial);
	}
	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.datatypes.ContextDefiner#clear()
	 */
	@Override
	public void clear() {
		ContextDatabase.getInstance().clear(this);
	}
	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.datatypes.ContextDefiner#get(int)
	 */
	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.datatypes.ContextDefiner#getAll()
	 */
	@Override
	public ArrayList<PageWrapper> getAll() {
		return ContextDatabase.getInstance().getAll(this);
	}

	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.datatypes.ContextDefiner#addOrUpdate(com.ofek.eventos.db.datatypes.PageDefiner)
	 */
	@Override
	public void addOrUpdate(final PageWrapper newPageDefiner) {
		ContextDatabase.getInstance().addOrUpdate(this,newPageDefiner);
	}
	@Override
	public void add(final PageDefiner newPageDefiner) {
		ContextDatabase.getInstance().add(this,newPageDefiner);
	}
	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.datatypes.ContextDefiner#remove(java.lang.Object)
	 */
	@Override
	public void remove(final PageWrapper object) {
		ContextDatabase.getInstance().remove(this,object);
	}

	private void init() {
		realDefiner = IOUtils.stringToObject(serial, ContextDefiner.class);
	}

	/* (non-Javadoc)
	 * @see com.ofek.eventos.db.datatypes.ContextDefiner#getName(android.content.Context)
	 */
	@Override
	public String getName(final Context c) {
		if (realDefiner==null) init();
		return realDefiner.getName(c);
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return getName(EventosApplication.context);
	}

	public ContextWrapper getParent() {
		return ContextDatabase.getInstance().getParent(this);
	}
}